package com.rover.controller.system;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.grid.Page;
import com.rover.entity.system.Code;
import com.rover.service.system.CodeService;
import com.utils.AppUtil;

/**
 * 用户
 */
@Controller
@RequestMapping("/codeController")
public class CodeController {

	@Autowired
	private CodeService codeService;
	
	// 用户管理
	@RequestMapping("/query")
	@ResponseBody
	public Map<String, Object> query(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		
		Map<String, Object> params = new HashMap<String, Object>();
		Page<Code> page = new Page<Code>(request);
		String loginName = (String) request.getParameter("name");
		String hql = "";
		if (null != loginName) {
			hql = "where name like :name";
			params.put("name", "%" + loginName + "%");
		}
		if(page.getOrderBy()==null){
		    hql += " order by id desc";
		}
		else{
			 hql += " order by "+page.getOrderBy()+" "+page.getOrderDir();
		}
		
		page = codeService.query(page, hql, params);
		map = AppUtil.buildJSONDataResponse(page.getResult(),
				page.getTotalCount());
		return map;
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> save(Code code, HttpServletRequest request)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		Boolean success = true;
		String message = "";
		try {
			if (null == code.getId()) {
				success = !codeService.isAddNameRepeat(code.getName());
			} else {
				success = !codeService.isEditNameRepeat(	code.getId(),code.getName());
			}
			// 判断是否验证成功
			if (success) {
				codeService.save(code);
			} else {
				message = "此编码名称已存在，请重新输入！";
			}
			map.put("success", success);
			map.put("message", message);
		} catch (Exception e) {
			// logger.error(e.getMessage(), e);
			map.put("success", false);
			map.put("message", e.getMessage());
		}
		return map;
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> delete(@RequestParam("id") String id) {
		Map<String, Object> map = new HashMap<String, Object>();
		// String id = request.getParameter("id");
		try {
			codeService.delete(Long.valueOf(id));
			map.put("success", true);
			map.put("message", "");

		} catch (Exception e) {
			map.put("success", false);
			map.put("message", e.getMessage());
		}
		return map;
	}
	
	

}
